#ifndef _PARASE_ARGS_H
#define _PARASE_ARGS_H
#include "iostream"
#include "vector"
#include "map"
#include "string"
#include "functional"		// function object
#include "set"

namespace cmdline{
     
     enum KeyFlag{INVALID_KEY=-1, NO_VALUE, MAY_VALUE, MUST_VALUE};


     class ParaseArg{
	  
	  class Unit{
	  public:
	       std::set<std::string> value;
	       std::function<void(std::string)> func;
	       KeyFlag key;
	  }; 
     public:
	  ParaseArg(){}
	  ~ParaseArg(){}
	  
	  
	 bool Add(std::string id, Unit u);
 	 bool Add(std::string id,  std::function<void(std::string)> func, KeyFlag key, std::set<std::string> arg);
	 bool Check(std::string id, std::string value);
	 bool Parase(int argc, char *argv[]);
     private:
	  std::map<std::string,Unit> _rule;
     };
     /* 检查参数是否在范围内 */
     
     bool ParaseArg::Check(std::string id, std::string value){
	  if(_rule[id].key == MUST_VALUE){
	       if(_rule[id].value.end() ==  _rule[id].value.find(value))
		    return false;	      
	  }
	  return true;
     }
     /* 添加待解析的参数 */
     
     bool ParaseArg::Add(std::string id, std::function<void(std::string)> func,  KeyFlag key, std::set<std::string> arg){
	  Unit u;
	  u.value = arg;
	  u.func = func;
	  u.key = key;

	  return Add(id, u);
     }
     bool ParaseArg::Add(std::string id, Unit u){
	  std::pair<std::map<std::string, Unit>::iterator, bool> res;
	  res = _rule.insert(std::pair<std::string, Unit>(id, u));
	  return res.second;
     }
     /* 解析参数意义，调用回调函数 */
     bool ParaseArg::Parase(int argc, char *argv[]){
	  int i=1;
	  for(;i<argc; i++){
	       auto it = _rule.find(argv[i]);
	       if(it != _rule.end()){
		    if(Check(argv[i], argv[i+1]))
			 _rule[argv[i]].func(argv[i+1]);
	       }
	  }
     }
}






#endif
